package net.hvacapp.cmvrfforandroid.ui.project;

import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.mirror.android.common.util.StringUtils;

import net.hvacapp.cmvrfforandroid.R;
import net.hvacapp.cmvrfforandroid.adapter.BaseAdapterHelper;
import net.hvacapp.cmvrfforandroid.adapter.QuickAdapter;
import net.hvacapp.cmvrfforandroid.http.BasicHttpClient;
import net.hvacapp.cmvrfforandroid.http.HttpCallbackHandle;
import net.hvacapp.cmvrfforandroid.http.HttpConstant;
import net.hvacapp.cmvrfforandroid.manager.AccountManager;
import net.hvacapp.cmvrfforandroid.manager.ProjectManager;
import net.hvacapp.cmvrfforandroid.ui.BaseActivity;
import net.hvacapp.cmvrfforandroid.ui.MainTabBarActivity;
import net.hvacapp.cmvrfforandroid.ui.project.model.GetProjectTempleteDataResp;
import net.hvacapp.cmvrfforandroid.ui.project.model.ProjectTempleteData;
import net.hvacapp.cmvrfforandroid.ui.selections.model.AreaData;
import net.hvacapp.cmvrfforandroid.utils.AnnotationUtil;
import net.hvacapp.cmvrfforandroid.widget.ProjectActionView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.HashMap;
import java.util.Map;

@EActivity(R.layout.activity_project_report)
public class ProjectReportActivity extends BaseActivity {

    @ViewById(R.id.action_view)
    ProjectActionView mActionView;


    private Dialog mProgressDlg;

    @ViewById(R.id.lv_templete)
    ListView mTempleteLv;

    private QuickAdapter<ProjectTempleteData> mQuickAdapter;

    private ProjectTempleteData mSelectedData;


    /**
     * 页面加载完后执行
     */
    @AfterViews
    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
        initViews();
        initEvent();
    }

    /***
     * 初始化视图
     */
    private void initViews() {
        mActionView.setActionInfo(ProjectManager.getInstance().getTotal(), ProjectManager.getInstance().getCurrent());
    }

    /**
     * 初始化
     */
    @Override
    public void initBoot() {
        setupToolbar(0, "项目报告", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(AnnotationUtil.get(MainTabBarActivity.class));
            }
        });
        if (mQuickAdapter == null){
            mQuickAdapter = new QuickAdapter<ProjectTempleteData>(context, R.layout.two_lines_cell_item) {
                @Override
                protected void convert(BaseAdapterHelper helper, ProjectTempleteData item) {

                    if (item.isSelected()) {
                        helper.setVisible(R.id.iv_selected_mark, true);
                    }
                    else {
                        helper.setVisible(R.id.iv_selected_mark, false);
                    }
                    helper.setText(R.id.tv_title, item.getTitle());
                    helper.setText(R.id.tv_description, item.getComment());
                }
            };
        }

        mTempleteLv.setAdapter(mQuickAdapter);
    }


    /**
     * 初始化数据
     */
    @Override
    public void initData(Intent intent) {
        loadProjectTemplete();
    }



    /**
     * 页面名称
     */
    @Override
    public String getName() {
        return "";
    }

    private void initEvent() {
        mActionView.setLeftOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //上一步
                ProjectManager.getInstance().goLastStep();
                finish();
            }
        });

        mActionView.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //下一步
//                ProjectManager.getInstance().goNextStep();
//                startActivity(AnnotationUtil.get(ProjectStep5Activity.class));
            }
        });

        mTempleteLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ProjectTempleteData data = (ProjectTempleteData) parent.getItemAtPosition(position);
                if (mSelectedData == null) {
                    ImageView ivSelectMark = (ImageView) view.findViewById(R.id.iv_selected_mark);
                    ivSelectMark.setVisibility(View.VISIBLE);
                    data.setSelected(true);
                    mSelectedData = data;
                } else {
                    if (StringUtils.isEquals(mSelectedData.getGuidId(), data.getGuidId())) {
                        ImageView ivSelectMark = (ImageView) view.findViewById(R.id.iv_selected_mark);
                        ivSelectMark.setVisibility(View.INVISIBLE);
                        data.setSelected(false);
                    } else {
                        mSelectedData.setSelected(false);
                        data.setSelected(true);
                        mSelectedData = data;
                        mQuickAdapter.notifyDataSetChanged();
                    }
                }
            }
        });


    }


    private void loadProjectTemplete() {
        BasicHttpClient.httpGetAsync(HttpConstant.HTTP_GET_PROJECT_TEMPLATE, AccountManager.getInstance().getToken(), new HttpCallbackHandle<GetProjectTempleteDataResp>() {
            @Override
            public void onSuccess(GetProjectTempleteDataResp obj) {
                // 成功
                mQuickAdapter.addAll(obj.getGetAllProjectTemplateResult());
            }

            @Override
            public void onFailure(String response) {
                showMessage("获取系统模板失败，请重试！");
            }
        });
    }


}
